#include "recordwidget.h"

RecordWidget::RecordWidget(QWidget *parent)
    :QWidget (parent)
{
    this->x=0;
    this->init_window();
    this->init_control();
    this->init_connect();
}

RecordWidget::RecordWidget(QList<QString> videoList, QString path)
{
    qDebug() << path;

    this->init_window();
    this->init_control();
    this->init_connect();
    this->decode->path = path;
    qDebug() << "this->decode->path =" <<  path;

    this->videoList = videoList;
    for(auto i=this->videoList.begin();i!=this->videoList.end();i++){
        qDebug() << "i =" << (*i);
        this->index++;
        //路径相同遍历结束
        if((*i)==this->decode->path){
            break;
        }
    }


}

void RecordWidget::init_window()
{
    this->setWindowTitle("录像回放界面");
    this->resize(700,700);

}

void RecordWidget::init_control()
{
    this->decode = new DecodeVideo();
    this->decode->setIsRun(false);
    this->decode->setIsStop(false);

    this->title = new QLabel("录像回放界面",this);
    this->title->move(250,50);
    this->title->setStyleSheet("font-size:36px");

    this->backBtn = new QPushButton("返回", this);
    this->backBtn->move(50,100);
//    this->backBtn->setFixedWidth(50);
    this->beginBtn = new QPushButton("开始", this);
    this->beginBtn->move(25,600);
    this->beginBtn->setFixedWidth(75);
    this->preBtn = new QPushButton("上一个", this);
    this->preBtn->move(175,600);
    this->preBtn->setFixedWidth(75);
    this->nextBtn = new QPushButton("下一个", this);
    this->nextBtn->move(250,600);
    this->nextBtn->setFixedWidth(75);

    this->btn2 = new QPushButton("2倍速", this);
    this->btn2->move(400, 600);
    this->btn2->setFixedWidth(75);
    this->btn5 = new QPushButton("5倍速", this);
    this->btn5->move(475,600);
    this->btn5->setFixedWidth(75);

    this->btn4 = new QPushButton("暂停", this);
    this->btn4->move(100,600);
    this->btn4->setFixedWidth(75);

    this->screenBtn = new QPushButton("截图", this);
    this->screenBtn->move(550,600);
    this->slider = new QSlider(this);
    this->slider->setMinimum(0);


//    VideoModel::getInstance()->selectFps(this->decode->path, qres, row, col);

//    this->slider->setMaximum(atoi(qres[1]));
    this->slider->setValue(0);
    this->slider->move(100,555);
    this->slider->setOrientation(Qt::Horizontal);
    this->slider->setSingleStep(1);
    this->slider->setFixedWidth(500);


    //先查询当前登录的用户
    char **qres = nullptr;
    int row=0,col=0;

    VideoModel::getInstance()->selectVideo(qres, row, col);




}

void RecordWidget::init_connect()
{
    connect(this->beginBtn, SIGNAL(clicked()), this, SLOT(on_button_click()));
    connect(this->btn2, SIGNAL(clicked()), this, SLOT(on_button2_click()));
    connect(this->btn5, SIGNAL(clicked()), this, SLOT(on_button3_click()));
    connect(this->btn4, SIGNAL(clicked()), this, SLOT(on_button4_click()));
    connect(this->decode, SIGNAL(sendImg(QImage)), this, SLOT(receiveImg(QImage)));
    connect(this->screenBtn, SIGNAL(clicked()), this, SLOT(on_button5_click()));
    connect(this->backBtn, SIGNAL(clicked()), this, SIGNAL(backToPalyWidget()));

    connect(this->preBtn, SIGNAL(clicked()), this, SLOT(preVideo()));
    connect(this->nextBtn, SIGNAL(clicked()), this, SLOT(nextVideo()));

    //滑块移动
    //connect(this->slider, SIGNAL(valueChanged(int)), this, SLOT(ValueChanged(int)));
}

void RecordWidget::closeEvent(QCloseEvent *event)
{
    QMessageBox::StandardButton resBtn = QMessageBox::question(this, "Confirm Exit",
                tr("Are you sure you want to exit?\n"),
                QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
                QMessageBox::Yes);

    if (resBtn != QMessageBox::Yes) {
        event->ignore();
    } else {
//        UserModel::getInstance()->updateUserAll();
        event->accept();
    }
}

void RecordWidget::paintEvent(QPaintEvent *event)
{
    //画笔对象
    QPainter paint(this);
    //判断Image是不是空的
    if(!this->image.isNull())
    {
        paint.drawImage(QRect(100,150,500,400), this->image);
    }
}

void RecordWidget::on_button_click()
{

    char **qres = nullptr;
    int row=0,col=0;
    VideoModel::getInstance()->selectFps(this->decode->path, qres, row, col);

    this->slider->setMaximum(atoi(qres[1]));

    this->decode->setIsRun(true);
    this->decode->setIsStop(false);
    this->decode->start();


}

void RecordWidget::receiveImg(QImage img)
{
    this->image = img;

    //手动触发paintEvent重绘事件
    update();
//    if(this->decode->getSpeed()==40)
//    {
//        emit this->slider->valueChanged(1);
//    }
//    else if (this->decode->getSpeed()==20) {
//        emit this->slider->valueChanged(2);
//    }
    this->x++;
    if(this->x>=this->slider->maximum())
    {
        this->x=0;
    }
    this->slider->setValue(x);

}

void RecordWidget::on_button2_click()
{
    //先暂停
    this->decode->setIsRun(false);
    this->decode->setSpeed(20);
    qDebug() << "2.0倍速";
    //恢复使用
    this->decode->setIsRun(true);
}

void RecordWidget::on_button3_click()
{
    //先暂停
    this->decode->setIsRun(false);
    this->decode->setSpeed(8);
    qDebug() << "5.0倍速";
    //恢复使用
    this->decode->setIsRun(true);
}

void RecordWidget::on_button4_click()
{
    if(this->btn4->text()=="暂停")
    {
        this->decode->setIsRun(false);
        this->btn4->setText("继续");
    }
    else{
        this->decode->setIsRun(true);
        this->btn4->setText("暂停");
    }
}

void RecordWidget::preVideo()
{
    this->decode->setIsRun(false);
    this->decode->setIsStop(true);
    this->x = 0;
    this->decode->path = this->videoList.at(--this->index);
//    char **qres;
//    int row=0,col=0;
//    VideoModel::getInstance()->selectFps(this->videoList.at(this->index), qres, row, col);

//    this->slider->setMaximum(atoi(qres[1]));
    this->decode->openVideo();

}

void RecordWidget::nextVideo()
{
    this->decode->setIsRun(false);
    this->decode->setIsStop(true);
    this->x = 0;
    this->decode->path = this->videoList.at(++this->index);
//    char **qres;
//    int row=0,col=0;
//    VideoModel::getInstance()->selectFps(this->videoList.at(this->index), qres, row, col);

//    this->slider->setMaximum(atoi(qres[1]));

    this->decode->openVideo();
}

void RecordWidget::on_button5_click()
{
    QMessageBox::information(this, "录像回放界面", "截图成功");
    //获取当前日期和时间
    QDateTime currentDateTime = QDateTime::currentDateTime();
    //获取当前时间
    QTime currentTime = currentDateTime.time();
    //获取当前日期
    QDate currentDate = currentDateTime.date();
    QString date = currentDateTime.toString("yyyy_MM_dd_hh_mm_ss");
    QString datetime = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
    this->image.save(QString("../screen/%1.jpg").arg(date));
    qDebug() << QString("../screen/%1.jpg").arg(date);

    char **qres;
    int col=0,row=0;

    UserModel::getInstance()->selectUserId(qres, row, col);
    QString name;
    if(atoi(qres[1])==1)
    {
        name = "100101";
    }
    else if(atoi(qres[1])==2) {
        name = "100102";
    }
    else {
        name = "100103";
    }
    qres = nullptr;
    col=0,row=0;
    ConfigureModel::getInstance()->selectLastConfigure(qres, row, col);
    LogModel::getInstance()->InsertLog(datetime, qres[1], name, "截图");

}

//void RecordWidget::ValueChanged(int val)
//{
//    qDebug() << "wwwwwwwwwwwwwwwwwwwwwwww";
//    this->slider->setValue(this->slider->value()+val);
//}
